package com.lbloca.locate.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.appcompat.widget.Toolbar;

import com.gyf.immersionbar.ImmersionBar;
import com.lbloca.locate.R;
import com.lbloca.locate.biz.Config;
import com.lbloca.locate.ui.base.BaseActivity;
import com.lbloca.locate.utils.PayFailureVerification;
import com.lbloca.locate.utils.PayUtils;
import com.lbloca.locate.utils.SPUtils;
import com.lbloca.locate.utils.ToastUtils;
import com.lbloca.locate.weight.BottomPayDialog;

import butterknife.BindView;
import butterknife.OnClick;

public class VipActivity extends BaseActivity {
    @BindView(R.id.id_toolbar)
    Toolbar mToolbar;
    @BindView(R.id.id_rl_month)
    RelativeLayout mRlMonth;
    @BindView(R.id.id_rl_month_select)
    RelativeLayout mRlMonthSelect;
    @BindView(R.id.id_iv_month_bg)
    ImageView mIvMonthBg;
    @BindView(R.id.id_rl_year)
    RelativeLayout mRlYear;
    @BindView(R.id.id_rl_year_select)
    RelativeLayout mRlYearSelect;
    @BindView(R.id.id_iv_year_bg)
    ImageView mIvYearBg;
    @BindView(R.id.id_rl_lifelong)
    RelativeLayout mRlLifeLong;
    @BindView(R.id.id_rl_lifelong_select)
    RelativeLayout mRlLifeLongSelect;
    @BindView(R.id.id_iv_lifelong)
    ImageView mIvLifeLong;
    @BindView(R.id.id_go_pay)
    TextView mTvGoPay;
    @BindView(R.id.id_tv_lifelong_old_price)
    TextView mTvLifelongOldPrice;
    @BindView(R.id.id_tv_year_old_price)
    TextView mTvYearOldPrice;
    @BindView(R.id.id_tv_month_old_price)
    TextView mTvMonthOldPrice;

    private PayUtils mPayUtils;

    private String mPayPrice =Config.PAY_VIP_LIFELONG_PRICE;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_vip;
    }

    @Override
    protected void initView() {
        mTvLifelongOldPrice.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG);
        mTvYearOldPrice.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG);
        mTvMonthOldPrice.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initEvents() {

    }

    @OnClick({R.id.id_rl_back,R.id.id_rl_month,R.id.id_rl_year,R.id.id_rl_lifelong,R.id.id_go_pay})
    void onClick(View view) {
        switch (view.getId()){
            case R.id.id_rl_back:
                VipActivity.this.finish();
                break;
            case R.id.id_rl_month:
                clearSelectState();
                mIvMonthBg.setBackgroundResource(R.mipmap.beijing1);
                mRlMonthSelect.setVisibility(View.VISIBLE);
                mPayPrice =Config.PAY_VIP_MONTH_PRICE;
                break;
            case R.id.id_rl_year:
                clearSelectState();
                mIvYearBg.setBackgroundResource(R.mipmap.beijing1);
                mRlYearSelect.setVisibility(View.VISIBLE);
                mPayPrice =Config.PAY_VIP_YEAR_PRICE;
                break;
            case R.id.id_rl_lifelong:
                clearSelectState();
                mIvLifeLong.setBackgroundResource(R.mipmap.beijing1);
                mRlLifeLongSelect.setVisibility(View.VISIBLE);
                mPayPrice =Config.PAY_VIP_LIFELONG_PRICE;
                break;
            case R.id.id_go_pay:
                goPay();
                break;
            default:
                break;
        }
    }

    private void goPay() {
        if (Integer.valueOf(Config.PAY_VIP_LIFELONG_PRICE) > 0) {
            if (mPayUtils == null) {
                mPayUtils = new PayUtils(this);
                mPayUtils.setIPayUtilCallback(new PayUtils.IPayUtilCallback() {
                    @Override
                    public void onPaySuccess() {
                        SPUtils.getInstance().put(Config.SPF_VIP_STATE, 1);
                        ToastUtils.showShort("支付成功");
//                        tvGopay.setText("已开通");
//                        Intent intent = new Intent(VipActivity.this, MainActivity.class);
//                        intent.putExtra("tabindex", 0);
//                        startActivity(intent);
                    }

                    @Override
                    public void onPayFailure() {
                        // 再次查询
                        new PayFailureVerification(VipActivity.this);
                    }
                });
            }
            final BottomPayDialog dialog = new BottomPayDialog(this);
            dialog.setIOnClickListener(new BottomPayDialog.IOnClickListener() {
                @Override
                public void onItemClick(int position) {
                    if (position == 0)
                        mPayUtils.payMoney(Config.PAY_VIP, Config.PAY_VIP_LIFELONG_PRICE, Config.PAY_VIP, Config.PAYTYPE_ALI, Config.TYPE_VIP);
                    else
                        mPayUtils.payMoney(Config.PAY_VIP, Config.PAY_VIP_LIFELONG_PRICE, Config.PAY_VIP, Config.PAYTYPE_WEIXIN, Config.TYPE_VIP);
                    dialog.dismiss();
                }
            });
            dialog.show();
        } else {
            SPUtils.getInstance().put(Config.SPF_VIP_STATE, 1);
//            tvGopay.setText("已开通");
            Intent intent = new Intent(VipActivity.this, MainActivity.class);
            intent.putExtra("tabindex", 0);
            startActivity(intent);
        }
    }


    private void clearSelectState(){
        mIvLifeLong.setBackgroundResource(R.mipmap.beisebj);
        mIvYearBg.setBackgroundResource(R.mipmap.beisebj);
        mIvMonthBg.setBackgroundResource(R.mipmap.beisebj);
        mRlYearSelect.setVisibility(View.GONE);
        mRlMonthSelect.setVisibility(View.GONE);
        mRlLifeLongSelect.setVisibility(View.GONE);
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this)
                .statusBarDarkFont(true)
                .navigationBarDarkIcon(true)
                .titleBar(mToolbar)
                .init();
    }


    public static void start(Context context){
        Intent intent = new Intent(context,VipActivity.class);
        context.startActivity(intent);
    }
}
